﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyRapChieuPhim.BUS;
using QuanLyRapChieuPhim.DTO;

namespace QuanLyRapChieuPhim.GUI
{
    public partial class uscThemTaiKhoanKhachHang : UserControl
    {
        int Ma = -1;
        bool IsLoaded = false;
        string action = "";

        public uscThemTaiKhoanKhachHang()
        {
            InitializeComponent();
        }

        private void uscThemTaiKhoanKhachHang_Load(object sender, EventArgs e)
        {
            IsLoaded = false;
            Ma = int.Parse(Tag.ToString());
            LoadDefault();
            IsLoaded = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            action = "Add";
            Ma = 0;
            if (!Check())
                return;
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            TaiKhoanDTO DTO = new TaiKhoanDTO
                (
                    Ma, txtTenTaiKhoan.Text, txtMatKhau.Text, int.Parse(cbLoaiTaiKhoan.SelectedValue.ToString()),
                    int.Parse(cbNguoiDung.SelectedValue.ToString()), 0
                );
            if (BUS.Add_KhachHang(DTO))
            {
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);


        }
    
        private void btnCapNhap_Click(object sender, EventArgs e)
        {
            action = "Update";
            Ma = int.Parse(Tag.ToString());
            if (!Check())
                return;
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            TaiKhoanDTO DTO = new TaiKhoanDTO
                (
                    Ma, txtTenTaiKhoan.Text, txtMatKhau.Text, int.Parse(cbLoaiTaiKhoan.SelectedValue.ToString()),
                    int.Parse(cbNguoiDung.SelectedValue.ToString()), 0
                );
            if (BUS.Update_KhachHang(DTO))
            {
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);


        }

        private void btnDienLai_Click(object sender, EventArgs e)
        {
            LoadDefault();
        }

        void LoadDefault()
        {
            try
            {
                TaiKhoanDTO DTO = new TaiKhoanBUS().GetFromId_KhachHang(Ma);
                txtTenTaiKhoan.Text = DTO.TenDangNhap;
                txtMatKhau.Text = DTO.MatKhau;

                cbLoaiTaiKhoan.DataSource = new ChucVuBUS().GetList();
                cbLoaiTaiKhoan.DisplayMember = "TenChucVu";
                cbLoaiTaiKhoan.ValueMember = "MaChucVu";
                cbLoaiTaiKhoan.SelectedValue = DTO.MaLoaiTaiKhoan;

                cbNguoiDung.DataSource = new KhachHangBUS().GetList();
                cbNguoiDung.DisplayMember = "TenKhachHang";
                cbNguoiDung.ValueMember = "MaKhachHang";
                cbNguoiDung.SelectedValue = DTO.MaKhachHang;

            }
            catch (Exception)
            {
                txtTenTaiKhoan.Text = "";
                txtMatKhau.Text = "";

                cbLoaiTaiKhoan.DataSource = new ChucVuBUS().GetList();
                cbLoaiTaiKhoan.DisplayMember = "TenChucVu";
                cbLoaiTaiKhoan.ValueMember = "MaChucVu";
                cbLoaiTaiKhoan.SelectedIndex = 0;

                cbNguoiDung.DataSource = new KhachHangBUS().GetList();
                cbNguoiDung.DisplayMember = "TenKhachHang";
                cbNguoiDung.ValueMember = "MaKhachHang";
                cbNguoiDung.SelectedIndex = 0;
            }
            finally
            {
                txtTenTaiKhoan.Focus();
            }
        }

        bool Check()
        {
            lbsao01.Visible = false;
            lbsao02.Visible = false;
            lbThongBao.Visible = false;
            lbThongBao1.Visible = false;

            if (txtTenTaiKhoan.Text == "")
            {
                lbsao01.Visible = true;
                txtTenTaiKhoan.Focus();
                return false;
            }
            if (txtMatKhau.Text == "")
            {
                lbsao02.Visible = true;
                txtMatKhau.Focus();
                return false;
            }
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            if (action == "Add")
            {
                if (BUS.GetIDFromName(txtTenTaiKhoan.Text) > 0)
                {
                    lbThongBao.Visible = true;
                    txtTenTaiKhoan.Focus();
                    return false;
                }
                else if (BUS.GetIDFromIDKhachHang(int.Parse(cbNguoiDung.SelectedValue.ToString())) > 0)
                {
                    lbThongBao1.Visible = true;
                    cbNguoiDung.Focus();
                    return false;
                }
            }
            else if (action == "Update")
            {
                if (BUS.GetIDFromName(txtTenTaiKhoan.Text) > 0
                && BUS.GetIDFromName(txtTenTaiKhoan.Text) != Ma)
                {
                    lbThongBao.Visible = true;
                    txtTenTaiKhoan.Focus();
                    return false;
                }
            }
            return true;
        }

        private void lbThongBao_Click(object sender, EventArgs e)
        {

        }

        private void cbNguoiDung_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbLoaiTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void txtMatKhau_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTenTaiKhoan_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void lbsao02_Click(object sender, EventArgs e)
        {

        }

        private void lbThongBao1_Click(object sender, EventArgs e)
        {

        }
    
    }
}
